<?php
// Functions that format strings -- From Matt Mullenweg (ma.tt)
function abbr($text) {
	$acronyms = array(
		'WYSIWYG' => 'what you see is what you get',
		'XHTML' => 'eXtensible HyperText Markup Language',
		'IIRC' => 'if I remember correctly',
		'HDTV' => 'High Definition TeleVision',
		'LGPL' => 'GNU Lesser General Public License',
		'MSDN' => 'Microsoft Developer Network',
		'WCAG' => 'Web Content Accessibility Guidelines',
		'SOAP' => 'Simple Object Access Protocol',
		'OPML' => 'Outline Processor Markup Language',
		'MSIE' => 'Microsoft Internet Explorer',
		'FOAF' => 'Friend of a Friend vocabulary',
		'GFDL' => 'GNU Free Documentation License',
		'XSLT' => 'eXtensible Stylesheet Language Transformation',
		'HTML' => 'HyperText Markup Language',
		'IHOP' => 'International House of Pancakes',
		'IMAP' => 'Internet Message Access Protocol',
		'RAID' => 'Redundant Array of Independent Disks',
		'HPUG' => 'Houston Palm Users Group',
		'VNC' => 'Virtual Network Computing',
		'URL' => 'Uniform Resource Locator',
		'W3C' => 'World Wide Web Consortium',
		'MSN' => 'Microsoft Network',
		'USB' => 'Universal Serial Bus',
		'P2P' => 'Peer To Peer',
		'PBS' => 'Public Broadcasting System',
		'RSS' => 'Rich Site Summary',
		'SIG' => 'Special Interest Group',
		'RDF' => 'Resource Description Framework',
		'AOL' => 'American Online',
		'PHP' => 'PHP Hypertext Processor',
		'SSN' => 'Social Security Number',
		'JSP' => 'Java Server Pages',
		'DOM' => 'Document Object Model',
		'DTD' => 'Document Type Definition',
		'DVD' => 'Digital Video Disc',
		'DNS' => 'Domain Name System',
		'CSS' => 'Cascading Style Sheets',
		'CGI' => 'Common Gateway Interface',
		'CMS' => 'Content Management System',
		'FAQ' => 'Frequently Asked Questions',
		'FSF' => 'Free Software Foundation',
		'API' => 'Application Interface',
		'PDF' => 'Portable Document Format',
		'IIS' => 'Internet Infomation Server',
		'XML' => 'eXtensible Markup Language',
		'XSL' => 'eXtensible Stylesheet Language',
		'GPL' => 'GNU General Public License',
		'KDE' => 'K Desktop Environment',
		'IE' => 'Internet Explorer',
		'CD' => 'Compact Disk',
		'TB' => 'Terabyte',
		'GB' => 'Gigabyte',
		'MB' => 'Megabyte',
		'KB' => 'Kilobyte'
	);
	
	//uksort($acronyms, 'sortr_longer'); // comment out if already sorted
	foreach ($acronyms as $acronym => $definition) {
		$text = preg_replace("#$acronym(?!</(ac|sp))#", "<acronym title=\"$definition\">$acronym</acronym>", $text, 1);
		$text = preg_replace("#$acronym(?!</(ac|sp))#",strtoupper($acronym), $text);
	}
	return $text;
}

function autop($pee, $br = 1) {
	$pee = $pee . "\n"; // just to make things a little easier, pad the end
	$pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
	// Space things out a little
	$allblocks = '(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|map|area|blockquote|address|math|style|input|p|h[1-6]|hr)';
	$pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee);
	$pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
	$pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
	if ( strpos($pee, '<object') !== false ) {
		$pee = preg_replace('|\s*<param([^>]*)>\s*|', "<param$1>", $pee); // no pee inside object/embed
		$pee = preg_replace('|\s*</embed>\s*|', '</embed>', $pee);
	}
	$pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
	$pee = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "<p>$1</p>\n", $pee); // make paragraphs, including one at the end
	$pee = preg_replace('|<p>\s*?</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
	$pee = preg_replace('!<p>([^<]+)\s*?(</(?:div|address|form)[^>]*>)!', "<p>$1</p>$2", $pee);
	$pee = preg_replace( '|<p>|', "$1<p>", $pee );
	$pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
	$pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
	$pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
	$pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
	$pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
	$pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
	if ($br) {
		$pee = preg_replace_callback('/<(script|style).*?<\/\\1>/s', create_function('$matches', 'return str_replace("\n", "<WPPreserveNewline />", $matches[0]);'), $pee);
		$pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
		$pee = str_replace('<WPPreserveNewline />', "\n", $pee);
	}
	$pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee);
	$pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
	if (strpos($pee, '<pre') !== false)
		$pee = preg_replace_callback('!(<pre.*?>)(.*?)</pre>!is', 'clean_pre', $pee );
	$pee = preg_replace( "|\n</p>$|", '</p>', $pee );
	//$pee = preg_replace('/<p>\s*?(' . get_shortcode_regex() . ')\s*<\/p>/s', '$1', $pee); // don't auto-p wrap shortcodes that stand alone

	return $pee;
}

function clean_pre($matches) {
	if (is_array($matches))
		$text = $matches[1].$matches[2] . '</pre>';
	else
		$text = $matches;

	$text = str_replace('<br />', '', $text);
	$text = str_replace('<p>', "\n", $text);
	$text = str_replace('</p>', '', $text);
	
	return $text;
}

function get_excerpt($text = '', $addon = '[...]', $length = 55) {
	if ($text == '') return;
	
	$words = explode(' ', strip_tags(str_replace(']]>', ']]&gt;', $text)), $length + 1);
	
	if (count($words) > $length) {
		array_pop($words);
		array_push($words, $addon);
		$text = implode(' ', $words);
	}
	
	return $text;
}
// End;

function noun2plural($word = '', $number = 1) {
	if (empty($word)) return false;
	$number = ((int) $number < 0) ? -((int) $number) : (int) $number;
	
	if ($number !== 1) {
		/* Weird words that don't follow the rule. */
		$irregular = array('aircraft' => 'aircraft', 'alga' => 'algae', 'alumna' => 'alumnae', 'buffalo' => 'buffalo', 'child' => 'children', 'deer' => 'deer', 'die' => 'dice', 'foot' => 'feet', 'goose' => 'geese', 'median' => 'media', 'man' => 'men', 'mouse' => 'mice', 'oasis' => 'oases', 'ox' => 'oxen', 'salmon' => 'salmon', 'sheep' => 'sheep', 'tooth' => 'teeth', 'woman' => 'women');
		foreach($irregular as $singular => $plural) if ($word == $singular) return $plural;
		
		if (preg_match('/[aeiou]{1}[y]$/i', $word)) return $word.'s'; // vowel + y
		elseif (preg_match('/[^aeiou]{1}[y]$/i', $word)) return preg_replace('/[y]$/i', 'ies', $word); //consonant + y
		elseif (substr($word, -2) == 'is') return preg_replace('/[is]$/i', 'es', $word); // is
		elseif (substr($word, -1) == 'f') return preg_replace('/[f]$/i', 'ves', $word); // f
		elseif (substr($word, -2) == 'fe') return preg_replace('/[ef]$/i', 'ves', $word); // fe
		else {
			$sounds = array('s', 'x', 'z', 'ch', 'sh');
			foreach($sounds as $sound) if (substr($word, -strlen($sound)) == $sound) return $word.'es';
		}
		
		return $word.'s';
	} else
		return $word;
}
?>